/*
 * ChooseAddressDialog.java
 * business
 *
 * Created by ChenTao on 2017/3/13.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.address;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.address.Area;
import com.yidiandao.business.data.model.AddressModel;
import com.yidiandao.business.data.model.IAddressModel;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择地址的Dialog
 *
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/3/11 0011 10:21
 */
public class ChooseAddressDialog extends Dialog {

    private IAddressModel mAddressModel;

    private ImageView mIvClose;

    private ImageView mLoadingView;

    private TabLayout mTabLayout;

    private ViewPager mViewPager;

    // For View Pager
    private AddressPagerAdapter mPagerAdapter;

    private OnAddressChangeListener mAddressChangedListener;

    private List<AddressPager> mAddressPagers;

    private AddressPager mProvincePager;

    private AddressPager mCityPager;

    private AddressPager mDistrictPager;

    public ChooseAddressDialog(Context context) {
        super(context, R.style.BottomDialogTheme);
        mAddressModel = new AddressModel();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_choose_address);

        initWindow();
        initViews();
        setupAnimation();
        initPagers();
        initListeners();
    }

    private void setupAnimation() {
        AnimationDrawable drawable = (AnimationDrawable) mLoadingView.getDrawable();
        drawable.start();
    }

    private void initWindow() {
        // reset window style
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;

        window.setAttributes(params);
    }

    private void initViews() {
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mIvClose = (ImageView) findViewById(R.id.iv_close);
        mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
        mLoadingView = (ImageView) findViewById(R.id.loading_view);
    }

    private void initPagers() {
        mAddressPagers = new ArrayList<>();

        mProvincePager = new AddressPager(mViewPager);
        mCityPager = new AddressPager(mViewPager);
        mDistrictPager = new AddressPager(mViewPager);

        mProvincePager.setInitTitle(UIUtils.getString(R.string.choose_province));
        mCityPager.setInitTitle(UIUtils.getString(R.string.choose_city));
        mDistrictPager.setInitTitle(UIUtils.getString(R.string.choose_district));

        mProvincePager.setNextPager(mCityPager);
        mCityPager.setNextPager(mDistrictPager);

        mAddressPagers.add(mProvincePager);

        mPagerAdapter = new AddressPagerAdapter(mAddressPagers);

        // 初始化ViewPager
        mViewPager.setAdapter(mPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void initListeners() {
        mIvClose.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });

        mDistrictPager.setOnFinalPagerItemClickListener(
                new AddressPager.OnFinalPagerItemClickListener() {
                    @Override
                    public void onFinalPagerItemClick() {

                        dismiss();

                        Area province = mProvincePager.getSelectedData();
                        Area city = mCityPager.getSelectedData();
                        Area district = mDistrictPager.getSelectedData();

                        if (mAddressChangedListener != null) {
                            mAddressChangedListener.onSelectedAddressChanged(
                                    province, city, district);
                        }
                    }
                });
    }

    /**
     * 设置默认地址
     *
     * @param provinceId 无默认则设置为-1,必须按层级设置
     * @param cityId 无默认则设置为-1,必须按层级设置
     * @param districtId 无默认则设置为-1,必须按层级设置
     */
    public void setDefaultAddress(int provinceId, int cityId, int districtId) {

        if (!isShowing()) {
            throw new IllegalStateException("you must call dialog.show() first");
        }

        mProvincePager.setDefaultId(provinceId);
        mCityPager.setDefaultId(cityId);
        mDistrictPager.setDefaultId(districtId);
    }

    @Override
    public void show() {
        super.show();
        getProvinceList();
    }

    private void hideLoadingView() {
        mLoadingView.setVisibility(View.GONE);
    }

    /** 获取所有的省 */
    private void getProvinceList() {
        getAddressListByRegionId(
                "1",
                new IAddressModel.OnAddressListener() {
                    @Override
                    public void onSuccess(List<Area> cities) {

                        hideLoadingView();

                        mProvincePager.setRecyclerViewData(cities);

                        // 状态回显
                        if (mProvincePager.getDefaultId() >= 0) {
                            refreshAddressUIWithOrigin();
                        }
                    }
                });
    }

    private void refreshAddressUIWithOrigin() {
        mProvincePager.autoSelectItemWithOriginId();
    }

    private void getAddressListByRegionId(
            String regionId, IAddressModel.OnAddressListener listener) {
        mAddressModel.getAreaList(regionId, ChooseAddressDialog.class.getSimpleName(), listener);
    }

    public void setOnAddressChangeListener(OnAddressChangeListener listener) {
        this.mAddressChangedListener = listener;
    }

    public interface OnAddressChangeListener {
        void onSelectedAddressChanged(Area province, Area city, Area district);
    }
}
